package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
)

type projectRepository struct {
}

// NewProjectRepository
// 找项目
func NewProjectRepository() ProjectRepositoryIface {
	return &projectRepository{}
}

// SearchProject 搜索项目
func (r *projectRepository) SearchProject(ctx context.Context, request *pb.SearchProjectRequest) (*pb.SearchProjectResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.SearchProject(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SearchProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// FuzzySearchProject 模糊搜索项目
func (r *projectRepository) FuzzySearchProject(ctx context.Context, request *pb.FuzzySearchProjectRequest) (*pb.FuzzySearchProjectResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearchProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.FuzzySearchProject(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "FuzzySearchProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ProjectDetail 搜索项目
func (r *projectRepository) ProjectDetail(ctx context.Context, request *pb.ProjectDetailRequest) (*pb.ProjectDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ProjectDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ProjectDetailLand 项目详情 - 拿地列表
func (r *projectRepository) ProjectDetailLand(ctx context.Context, request *pb.ProjectDetailLandRequest) (*pb.ProjectDetailLandResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailLand request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ProjectDetailLand(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailLand request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ProjectDetailBuild 项目详情 - 在建列表
func (r *projectRepository) ProjectDetailBuild(ctx context.Context, request *pb.ProjectDetailBuildRequest) (*pb.ProjectDetailBuildResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailBuild request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ProjectDetailBuild(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailBuild request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ProjectDetailSale 项目详情 - 开盘列表
func (r *projectRepository) ProjectDetailSale(ctx context.Context, request *pb.ProjectDetailSaleRequest) (*pb.ProjectDetailSaleResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailSale request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ProjectDetailSale(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailSale request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ProjectDetailBid 项目详情 - 招中标列表
func (r *projectRepository) ProjectDetailBid(ctx context.Context, request *pb.ProjectDetailBidRequest) (*pb.ProjectDetailBidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ProjectDetailBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ProjectDetailBid request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// TakenLandDetail 拿地详情
func (r *projectRepository) TakenLandDetail(ctx context.Context, request *pb.TakenLandDetailRequest) (*pb.TakenLandDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "TakenLandDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.TakenLandDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "TakenLandDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// SaleList 开盘列表
func (r *projectRepository) SaleList(ctx context.Context, request *pb.SaleListRequest) (*pb.SaleListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaleList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.SaleList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaleList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ExportSaleList 开盘列表-导出
func (r *projectRepository) ExportSaleList(ctx context.Context, request *pb.ExportSaleListRequest) (*pb.ExportSaleListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExportSaleList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ExportSaleList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExportSaleList request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// SaleDetail 开盘详情
func (r *projectRepository) SaleDetail(ctx context.Context, request *pb.SaleDetailRequest) (*pb.SaleDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaleDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.SaleDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaleDetail request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// NewHouse 新房销售
func (r *projectRepository) NewHouse(ctx context.Context, request *pb.NewHouseRequest) (*pb.NewHouseResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "NewHouse request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.NewHouse(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "NewHouse request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetTopProject 获取项目主体
func (r *projectRepository) GetTopProject(ctx context.Context, request *pb.GetTopProjectRequest) (*pb.GetTopProjectResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTopProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.GetTopProject(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTopProject request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

func (r *projectRepository) GetTakenLand(ctx context.Context, request *pb.GetTakeLandListRequest) (*pb.GetTakeLandListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.GetTakeLandList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTakenLand-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *projectRepository) ExportTakenLand(ctx context.Context, request *pb.ExportTakeLandListRequest) (*pb.ExportTakeLandListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewProjectServiceClient(conn)
	res, err := client.ExportTakeLandList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExportTakenLand-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}
